package queryProccessor;

import java.util.Scanner;
import java.util.StringTokenizer;

public class Validation {
	private Scanner scanner;
	private String[] statements;
	int type;

	public Validation() {
		this.type=0;
		scanner = new Scanner(System.in);
		takeInput();
		divide();
		
	}

	public void takeInput() {
		String s = scanner.nextLine();
		String str = "";
		while (!s.equals("0")) {
			str += s + " ";
			s = scanner.nextLine();
		}
		toArray(str);
	}
	
	private boolean validSyntax(String query) {

		String where = "\\s+(where|WHERE)\\s+\\w+\\s*(([><]|(<>))\\s*\\d+|(=\\s*('\\w*'|\\d+)))"
				+ "(\\s+(AND|OR)\\s+\\w+\\s*(([><]|(<>))\\s*\\d+|(=\\s*('\\w*'|\\d+))))?";

		// for INSERT
		if (query.matches("\\s*(insert|INSERT)\\s+(into|INTO)\\s+\\w+"
				+ "\\s*(\\(\\s*\\w+(\\s*,\\s*\\w*\\s*)*\\)"
				+ ")?\\s+(values|VALUES)\\s*\\("
				+ "\\s*('\\w*'|\\d+)\\s*(\\s*,\\s*('\\w*'|\\d+)\\s*)*\\)")) {
			type = 1;
			return true;
		}

		// for UPDATE
		if (query
				.matches("\\s*(update|UPDATE)\\s+\\w+\\s+(SET|set)"
						+ "\\s+(\\w+\\s*=\\s*('\\w*'|\\d+))(\\s*,\\s*\\w+\\s*=\\s*('\\w*'|\\d+)\\s*)*"
						+ "(" + where + ")?")) {
			type = 3;
			return true;
		}

		// for DELETE column
		if (query.matches("\\s*(DELETE|delete)\\s+(FROM|from)\\s+\\w+(" + where
				+ ")?")) {
			type = 4;
			return true;
		}

		// for SELECT 
		if (query.matches("(SELECT|select)\\s+(\\*|(\\s*\\w+(\\s*,\\s*\\w*\\s*)*))"
						+ "\\s+(FROM|from)\\s+\\w+(" + where + ")?")) {
			type = 2;
			return true;
		}

		return true;

	}

	private void toArray(String s) {
		StringTokenizer str = new StringTokenizer(s.trim(), ";");
		statements = new String[str.countTokens()];
		int i = 0;
		while (str.hasMoreTokens()) {
			statements[i] = str.nextToken().trim();
			i++;
		}
		for (int j = 0; j < statements.length; j++) {
			System.out.println(statements[j]);
		}

	}

	private void divide() {
		Command command;
		
		
		
		
		for (int i = 0; i < statements.length; i++) {
			if ((validSyntax(statements[i]))==true&&type==1) {
				
			command = new Insert(statements[i] + ";");
			Insert n=new Insert(statements[i] + ";");
			System.out.println(n.getTable_name()+"");
				for(int j=0;j<n.getColumns().length;j++){
					System.out.println(n.getColumns()[j]+":)"+n.getValues()[j]);
				}
				
				
				// here send parameters of insert to ghareeb
			} else if (validSyntax(statements[i])==true&&type==2) {
				command = new Select(statements[i] + ";");
				Select n=new Select(statements[i] + ";");
				System.out.println(n.getTable_name()+"/");
				System.out.println(n.getCondition());
				for(int j=0;j<n.getColumns().length;j++){
					System.out.println(n.getColumns()[j]+":)");
				}
				// here send parameters of insert to ghareeb
			} else if (validSyntax(statements[i])==true&&type==4) {
				command = new Delete(statements[i] + ";");
				Delete n=new Delete(statements[i] + ";");
				System.out.println(n.getTable_name()+"/");
					System.out.println(n.getCondition());
				// here send parameters of insert to ghareeb
			} else if (validSyntax(statements[i])==true&&type==3) {
				command = new Update(statements[i] + ";");
				Update n=new Update(statements[i] + ";");
				System.out.println(n.getTable_name()+"/");
				System.out.println(n.getCondition());
				for(int j=0;j<n.getColumns().length;j++){
					System.out.println(n.getColumns()[j]+":)"+n.getValues()[j]);
				}
				// here send parameters of insert to ghareeb
			}else
				System.out.println("wrong syntax :"+type);
		}
	}

	public static void main(String[] args) {
		new Validation();
	}
}